<template>
    <div>
        <Card shadow>
            <iview-table
                ref="iviewTable"
                :span1=16
                :span2=8
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
        </Card>
        <Modal v-model="showModel" @on-ok="save" ok-text="保存" :closable='false' :mask-closable=true :width="800">
            <h3 slot="header" style="color:#2D8CF0">基本信息</h3>
            <Row justify="center" class="code-row-bg">
                <Col span="20" offset="2">
                    <Form :label-width="100" label-position="right">
                        <FormItem label="发病时间：">
                            <Input v-model="formItem.time" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="主诉内容：">
                            <Input v-model="formItem.content" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="发病情况：">
                            <Input v-model="formItem.situation" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="伴随症状：">
                            <Input v-model="formItem.symptom" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="治疗经过：">
                            <Input v-model="formItem.process" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="目前状况：">
                            <Input v-model="formItem.state" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="使用情况：">
                            <Input v-model="formItem.useSituation" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                        <FormItem label="其他情况：">
                            <Input v-model="formItem.otherSituation" :rows="2" :autosize="{minRows: 2}" type="textarea" placeholder="请输入内容..."></Input>
                        </FormItem>
                    </Form>
                </Col>
            </Row>
        </Modal>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    export default {
        components: {
            iviewTable
        },
        data () {
            return {
                baseURL: '/user/complaint',
                selectRows: [],
                showModel: false,
                datas: [],
                formItem: {
                    id: '',
                    patientId: '',
                    content: '',
                    time: '',
                    situation: '',
                    symptom: '',
                    process: '',
                    state: '',
                    useSituation: '',
                    otherSituation: '',
                    recordType: ''
                },
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/complaint',
                    defaultFilters: [
                        {'field': 'record_type', 'value1': 1, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: 600,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },{
                            title: '发病时间',
                            key: 'time',
                            className: 'demo-table-info-column'
                        },{
                            title: '主诉内容',
                            key: 'content',
                            className: 'demo-table-info-column'
                        },
                        {
                            title: '操作',
                            width: 150,
                            align: 'center',
                            render: (h, params) => {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'default'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.select(params.row.id)
                                            }
                                        }
                                    }, '选择')
                                ]);
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            };
        },
        methods: {
            events (item) {
                let path = item.path
                if (path === 'add') {
                    this.add()
                } else if (path === 'edit') {
                    this.edit()
                } else if (path === 'delete') {
                    this.delete()
                }
            },
            add () {
                this.showModel = true
            },
            edit () {
                if (this.selectRows.length === 1) {
                    let row = this.selectRows[0]
                    let query = {
                        row: row
                    }
                    this.$bus.toPath('/assets/details', query)
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            delete () {
                if (this.selectRows.length > 0) {
                    let ids = []
                    for (let row of this.selectRows) {
                        ids.push(row.id)
                    }
                    if (ids.length === 1) {
                        this.doDelete(ids)
                    } else {
                        this.$Modal.confirm({
                            title: '操作提示',
                            content: '<p>确认删除'+ids.length+'条记录?</p>',
                            onOk: () => {
                                this.doDelete(ids)
                            }
                        })
                    }
                } else {
                    this.$bus.alert('warning', '请至少选择一条记录')
                }
            },
            doDelete (ids) {
                let url = this.baseURL + '/delete'
                this.$ajax.post(url, ids).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', msg)
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                });
            },
            save () {
                this.formItem.recordType = 1
                this.formItem.patientId = this.$route.query.id
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', msg)
                        this.queryModel.defaultFilters[1] = {'field': 'patient_id', 'value1': this.$route.query.id, 'operate': '=='}
                        this.$refs.iviewTable.refresh()
                        this.$bus.init(this.formItem)
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            select (id) {
                this.$emit('selectComplaint', id)
            },
            refresh () {
                this.$refs.iviewTable.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.queryModel.defaultFilters[1] = {'field': 'patient_id', 'value1': this.$route.query.id, 'operate': '=='}
                this.refresh()
            }
        },
        mounted () {
            this.init()
        }
    }
</script>

<style>

</style>
